package application;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class DivePlanner {
	public static final String DIVE = "DIVE";
	public static final String SIT = "SIT";
	public static final String DECOMP = "DECOMP";

	private List<DivePlan> planner;
	private boolean planInProgress;
	private int userSIT, userDepth, userTime;

	private FirstNAUITable first;
	private SecondNAUITable second;
	private ThirdNAUITable third;
	private Map<Character, Map<Integer, Integer[]>> table;
	private char letterGroup;

	public DivePlanner() {
		planner = new ArrayList<DivePlan>();
		planInProgress = false;

		first = new FirstNAUITable();
		second = new SecondNAUITable();
		third = new ThirdNAUITable();

		userSIT = userDepth = userTime = 0;
	}

	public boolean createNewPlan() {
		if (planInProgress) {
			return false;
		}

		planner = new ArrayList<DivePlan>();
		planInProgress = true;

		return true;
	}

	public void endPlan() {
		planInProgress = false;
	}

	public List<DivePlan> getPlanner() {
		return planner;
	}

	// access table1
	private void addDive(int depth, int time) {
		first.setDepthAndTime(depth, time);

		if (first.getDecompression() > 0) {
		}
	}

	// access table2
	private void addDive(char letterGroup, int SIT) {
		second.setGroupAndSIT(letterGroup, SIT);
	}

	// access table3
	private void addDive(char letterGroup, int depth, int time) {
		third.setLetterGroupAndDepthAndTime(letterGroup, depth, time);

	}

	private void addPlan(int diveCase, int depth, int time) {
		planner.add(new DivePlan(diveCase, depth, time));
	}

	public NAUITable addFirstDive(int depth, int time) {
		first.setDepthAndTime(depth, time);

		this.addPlan(DivePlan.DIVE, depth, time);

		if (first.getDecompression() > 0) {
			this.addPlan(DivePlan.DECOMPRESSION, DivePlan.DECOMPRESSION_DEPTH,
					first.getDecompression());
		}

		return first;
	}

	public NAUITable addRepetitiveDive(NAUITable table) {
		if (table instanceof FirstNAUITable) {
			System.out.println("****Received Tab1");
			first = (FirstNAUITable) table;

			second.setGroupAndSIT(first.getLetterGroup(), this.userSIT);

			this.addPlan(DivePlan.SIT, DivePlan.SIT_DEPTH, this.userSIT);

			return addRepetitiveDive(second);
		} else if (table instanceof SecondNAUITable) {
			System.out.println("****Received Tab2");
			second = (SecondNAUITable) table;

			third.setLetterGroupAndDepthAndTime(second.getLetterGroup(),
					userDepth, userTime);

			this.addPlan(DivePlan.DIVE, userDepth, userTime);

			return third;
		} else if (table instanceof ThirdNAUITable) {
			System.out.println("****Received Tab3");
			third = (ThirdNAUITable) table;

			first.setDepthAndTime(third.getDepth(), third.getTime());

			if (first.getDecompression() > 0) {
				this.addPlan(DivePlan.DECOMPRESSION,
						DivePlan.DECOMPRESSION_DEPTH, first.getDecompression());
			}

			return addRepetitiveDive(first);
		}

		return table;
	}

	public void setUserSIT(int SIT) {
		this.userSIT = SIT;
	}

	public void setUserDepthAndTime(int depth, int time) {
		this.userDepth = depth;
		this.userTime = time;
	}

	public char getCurrLetterGroup() {
		return first.getLetterGroup();
	}

	public Map<Integer, Map<Character, Integer[]>> getFirstTable() {
		return first.getTable();
	}

	public Map<Character, Integer[]> getSecondTable() {
		return second.getTable();
	}

	public Map<Character, Map<Integer, Integer[]>> getThirdTable() {
		return third.getTable();
	}
}
